import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/Event.dart';
import 'package:flutter_demo/pages/message/MessagePage.dart';
import 'package:flutter_demo/pages/mine/MinePage.dart';
import 'package:flutter_demo/pages/task/TaskListPage.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    eventBus.on<TokenInvalidEvent>().listen((event) {
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        body: IndexedStack(
          index: _currentIndex,
          children: [TaskListPage(), MessagePage(), MinePage()],
        ),
        bottomNavigationBar: BottomNavigationBar(
          onTap: (value) {
            setState(() {
              _currentIndex = value;
            });
          },
          items: [
            BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/icon_btn_home.png',
                width: 30,
                height: 30,
              ),
              label: '首页',
            ),
            BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/icon_btn_message.png',
                width: 30,
                height: 30,
              ),
              label: '消息',
            ),
            BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/icon_btn_mine.png',
                width: 30,
                height: 30,
              ),
              label: '我的',
            ),
          ],
          currentIndex: _currentIndex,
        ),
      ),
    );
  }
}
